package net.starwifi.duobao.service.impl;

import net.starwifi.duobao.entity.DBOrder;
import net.starwifi.duobao.mvc.Filter;
import net.starwifi.duobao.mvc.Order;
import net.starwifi.duobao.mvc.Page;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.DBOrderService;
import net.starwifi.duobao.service.RefundHelperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * Created by Administrator on 2015/12/30.
 */

@Service
public class RefundHelperServiceImpl implements RefundHelperService{

    @Autowired
    DBOrderService dbOrderService;

    @Override
    public void batchRefunds(HttpServletRequest request){
        PageView pageView=new PageView(0,50);
        pageView.addFilter(Filter.eq("paymentStatus", 1));
        pageView.addFilter(Filter.eq("isRefund", Boolean.FALSE));
        pageView.addFilter(Filter.eq("codeInfo", ""));
        pageView.addFilter(Filter.gt("amount", BigDecimal.valueOf(0)));
        pageView.addOrder(Order.desc("id"));
        pageView.setNumPerPage(50); //每次查询20行
        Long total=dbOrderService.getMQueryCount(pageView.getFilters()); //记录总数
        int totalPage=(int) Math.ceil((double) total / (double) 50); //总页数
        for(int i=1;i<=totalPage;i++){
            pageView.setPageNumber(i); //当前页
            Page<DBOrder> page = dbOrderService.getScrollData(pageView);
            for (DBOrder data : page.getContent()) {
                try {
                    dbOrderService.refundsTr(data, request);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        System.out.println("---------------------------refunds is over");
    }



}
